package com.easymin.weixin.entity;

import com.easymin.weixin.common.weixinSecure.AesException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.io.StringReader;

/**
 * @author zhyu
 * @date 2019-07-14 16:00:48
 */
public class WeixinXmlEntity {

    private String Encrypt;
    private String tousername;
    private String fromusername;
    private Long createtime;
    private String msgtype;
    private String event;
    private String eventkey;
    private String ticket;
    private String content;
    private String msgId;


    public static final String SUBSCRIBE = "subscribe"; //关注公众号
    public static final String SCAN = "SCAN"; //公众号扫码
    public static final String UNSUBSCRIBE = "unsubscribe"; //取消关注公众号

    public enum EntityName {
        Encrypt(String.class),
        ToUserName(String.class),
        FromUserName(String.class),
        CreateTime(long.class),
        MsgType(String.class),
        Event(String.class),
        EventKey(String.class),
        Ticket(String.class),
        Content(String.class),
        MsgId(String.class),
        ;
        private Class type;

        private EntityName(Class type) {
            this.type = type;
        }
    }

    public static WeixinXmlEntity parseStreamToEntity(String text) {
        Object[] result = new Object[3];
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
            dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            dbf.setXIncludeAware(false);
            dbf.setExpandEntityReferences(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(text));
            Document document = db.parse(is);

            Element root = document.getDocumentElement();
            NodeList encrypt = root.getElementsByTagName("Encrypt");
            NodeList tousername = root.getElementsByTagName("ToUserName");
            NodeList fromusername = root.getElementsByTagName("FromUserName");
            NodeList createtime = root.getElementsByTagName("CreateTime");
            NodeList msgtype = root.getElementsByTagName("MsgType");
            NodeList event = root.getElementsByTagName("Event");
            NodeList eventkey = root.getElementsByTagName("EventKey");
            NodeList ticket = root.getElementsByTagName("Ticket");
            NodeList content = root.getElementsByTagName("Content");
            NodeList msgId = root.getElementsByTagName("MsgId");

            WeixinXmlEntity entity = new WeixinXmlEntity();
            entity.setTousername(getContent(tousername));
            entity.setFromusername(getContent(fromusername));
            entity.setEncrypt(getContent(encrypt));
            entity.setCreatetime(Long.valueOf(getContent(createtime)));
            entity.setMsgtype(getContent(msgtype));
            entity.setEvent(getContent(event));
            entity.setEventkey(getContent(eventkey));
            entity.setTicket(getContent(ticket));
            entity.setContent(getContent(content));
            entity.setMsgId(getContent(msgId));
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString() {
        return "WeixinXmlEntity{" +
                "Encrypt='" + Encrypt + '\'' +
                ", tousername='" + tousername + '\'' +
                ", fromusername='" + fromusername + '\'' +
                ", createtime=" + createtime +
                ", msgtype='" + msgtype + '\'' +
                ", event='" + event + '\'' +
                ", eventkey='" + eventkey + '\'' +
                ", ticket='" + ticket + '\'' +
                '}';
    }

    private static String getContent(NodeList nodeList) {
        if(null != nodeList) {
            Node item = nodeList.item(0);
            if(null != item){
                return item.getTextContent();
            }
        }
        return null;
    }

    public String getEncrypt() {
        return Encrypt;
    }

    public void setEncrypt(String encrypt) {
        Encrypt = encrypt;
    }

    public String getTousername() {
        return tousername;
    }

    public void setTousername(String tousername) {
        this.tousername = tousername;
    }

    public String getFromusername() {
        return fromusername;
    }

    public void setFromusername(String fromusername) {
        this.fromusername = fromusername;
    }

    public Long getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Long createtime) {
        this.createtime = createtime;
    }

    public String getMsgtype() {
        return msgtype;
    }

    public void setMsgtype(String msgtype) {
        this.msgtype = msgtype;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getEventkey() {
        return eventkey;
    }

    public void setEventkey(String eventkey) {
        this.eventkey = eventkey;
    }

    public String getTicket() {
        return ticket;
    }

    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }
}
